import { createStore, combineReducers, applyMiddleware } from 'redux'
import * as types from './types'
import thunk from 'redux-thunk'
/* 
thunk 中间件 可以让dispatch 接收一个函数作为action了

这个函数 会接收到对应dispatch

thunk 是让dispatch 执行的是把自身触发reducer的功能转接给了
对应的函数

*/

function reducer(state, action) {
  // action 就是一个普通对象 {type:'xxxx',xx:xx}
  // state就是我们的数据源
  console.log('userInfo的reducer')
  state = state || {
    name: "珠峰",
    age: 13
  }
  switch (action.type) {
    case types.CHANGENAME:
      state.name = action.nnn
      break;
    case types.CHANGEAGE:
      state.age = action.aaa
      break;
    case types.CHANGECOLOR2:
      state.name = action.ccc
  }
  return {
    ...state
  }
}
function colorReducer(state, action) {
  console.log('colorInfo的reducer')
  state = {
    color: 'red'
  }
  if (action.type == types.CHANGECOLOR) {
    state.color = action.ccc
  }
  return {
    ...state
  }
}
// 当有多个reducer的时候 我们需要合并reducer
let rootReducer = combineReducers({
  userInfo: reducer,
  colorInfo: colorReducer
})

let store = createStore(rootReducer, applyMiddleware(thunk))
export default store